<?php

namespace App\Models;

class PoolModel
{
    /**
     * 获取最新一条
     * return Array
     * */
    public static function getPoolInside($conn){
        $querySql="SELECT `id`,`prec`,`bonus`,`devote`,`user`,`sell`,`agent`,`area`,`city`,`pro` FROM `pool` WHERE  `state` = 1 order by `id` desc limit 1";
        return $conn->readConn($querySql);
    }
    /**
     * 获取奖励池总计
     * return Array
     * */
    public static function getPoolTotalInside($conn){
        $querySql="SELECT SUM(`bonus`) as bonus,SUM(`devote`) as devote,SUM(`user`) as users,SUM(`sell`) as sell,SUM(`agent`) as agent,sum(`area`) as area,sum(`city`) as city,sum(`pro`) as pro FROM `pool`";
        return $conn->readConn($querySql);
    }
    /**
     * 插入奖池信息
     * entry conn,token
     * return int
     * */
    public static function insertPoolInside($conn,$PoolExample){
        $insertSql = "insert into `pool`(`prec`,`bonus`,`devote`,`user`,`sell`,`agent`,`area`,`city`,`pro`)value($PoolExample->prec,$PoolExample->bonus,$PoolExample->devote,$PoolExample->user,$PoolExample->sell,$PoolExample->agent,$PoolExample->area,$PoolExample->city,$PoolExample->pro)";
        return $conn->executeConn($insertSql);
    }
    /**
     * 更新奖励
     * entry conn $PoolExample
     * return int
     * */
    public static function upPool($conn,$prec,$bonus,$devote,$user,$sell,$agent,$area,$city,$pro,$PoolExample){
        $sql = "UPDATE `pool` SET `prec` = `prec` + $prec,`bonus` = `bonus` + $bonus,`devote` = `devote` + $devote,`user` = `user` + $user,`sell` = `sell` + $sell,`agent` = `agent` + $agent,`area` = `area` + $area,`city` = `city` + $city,`pro` = `pro` + $pro WHERE `id` = $PoolExample->id and `prec` = $PoolExample->prec and `bonus` = $PoolExample->bonus and `devote` = $PoolExample->devote and `user` = $PoolExample->user and `sell` = $PoolExample->sell and `agent` = $PoolExample->agent and `area` = $PoolExample->area and `city` = $PoolExample->city and `pro` = $PoolExample->pro";
        return $conn->executeConn($sql);
    }
    /**
     * 编辑奖池信息
     * entry $WithdrawExample
     * return int
     * */
    public static function editPoolInside($conn,$PoolExample){
        $joinSql="";
        if($PoolExample->prec !=null){
            $joinSql.=" `prec` = '$PoolExample->prec',";
        }
        if($PoolExample->state !=null){
            $joinSql.=" `state` = '$PoolExample->state',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `pool` SET $innerJoin WHERE `id` =$PoolExample->id";
            return $conn->executeConn($upUserSql);
        }
    }
}